Ext.define('User', {
    extend: 'Ext.data.Model',
    fields: [
        "Id"
        , "Name"   //名称
        , "DepartID"   //所在部门ID
		, "Email"   //邮箱
		, "DisplayName"   //显示名称
    , "Slat"   //密码
		//, "PasswordHash"   //密码2
		, "CreateDate"   //创建时间
		, "ModifiedDate"   //修改时间
        , "Flag"   //状态				
    ],
    idProperty: "Id"
})

Ext.define('SHApp.Sys.User.Grid', {
    extend: 'BLBTSystem.grid',
    alias: 'widget.usergrid',
    requires: ['SHApp.Sys.User.Form'],
    txtNameID: 'Name',
    moduleId: "",
    columns: [{
        text: "Id",
        flex: 1,
        dataIndex: 'Id',
        sortable: false,
        hidden: true
    }, {
        text: "名称",
        width: 100,
        dataIndex: 'Name',
        sortable: true
    }, {
        text: "所在部门ID",
        width: 100,
        dataIndex: 'DepartID',
        sortable: true,
        hidden: true
    }, {
        text: "显示名称",
        width: 100,
        dataIndex: 'DisplayName',
        sortable: true
    }, {
        text: "邮箱",
        flex: 1,
        dataIndex: 'Email',
        sortable: true
    }, {
        text: "密码",
        width: 100,
        dataIndex: 'Slat',
        sortable: true,
        renderer: function () {
            return "******"
        },
        hidden: true
    }, {
        text: "创建时间",
        flex: 1,
        dataIndex: 'CreateDate',
        sortable: true,
        renderer: formatDate
    }, {
        text: "修改时间",
        flex: 1,
        dataIndex: 'ModifiedDate',
        sortable: true,
        renderer: formatDate
    }, {
        text: "状态",
        flex: 1,
        dataIndex: 'Flag',
        sortable: false,
        renderer: function (v) {
            if (v == 0) {
                return "解锁";
            } else if (v == 1) {
                return "锁定";
            }
        }
    }
    ],
    dataModel: "User",
    dataUrl:"api/user?format=json",
    loadByDepartID: function (departID) {
        var me = this;
        me.moduleId = departID;
        var store = me.store;
        me.clearFilters();
        me.addFilter("DepartID", departID);
        me.loadByFilter();
    },
    singleSelect: true,
    initComponent: function () {
        var me = this;
        me.btnAddID += me.id;
        me.btnEditID += me.id;
        me.btnDeleteID += me.id;
        me.btnUnLockID += me.id;
        me.btnLockID += me.id;
        me.txtNameID += "User";
        Ext.apply(this);
        this.creatTbar();
        this.callParent(arguments);
        this.checkBtnPermission(me.pvalue);
    },
    creatTbar: function () {
        var me = this;
        me.tbar = [{
            xtype: 'textfield',
            disabled: false,
            fieldLabel: '名称:',
            labelWidth: 40,
            emptyText: '',
            name: 'Name',
            id: me.txtNameID,
            width: 130
        }, {
            xtype: 'button',
            text: '查询',
            iconCls: 'icon-btn-search',
            handler: function () {
                var vName = Ext.getCmp(me.txtNameID).getValue();
                var store = me.store;
                //var DepartId = store.filters.items[0].value;

                me.clearFilters();
                if (me.moduleId != "") {
                    me.addFilter("DepartID", me.moduleId);
                }
                if (vName && vName.length > 0) {
                    me.addFilter("Name", vName);
                }
                me.loadByFilter();
            }
        }, '->', {
            xtype: 'button',
            text: '添加',
            id: me.btnAddID,
            iconCls: 'icon-btn-add',
            handler: function () {
                //var store = me.store;
                //var DepartId = store.filters.items[0];
                //if (DepartId && DepartId != null) {
                if (me.moduleId == "") {
                    Ext.Msg.alert("提示", "请先选择部门！");
                    return;
                }
                var win = me.getForm("SHApp.Sys.User.Form");
                win.down("#nameID").setVisible(true);
                win.loadRecord(new User({ CreateDate: new Date(), ModifiedDate: new Date(), Flag: 0, Id: null, DepartID: me.moduleId }));
                win.Show("添加");
                //} else {
                //   Ext.Msg.alert("提示信息", "请先选择所属店铺再添加。");
                //}
            }
        }, ' ', {
            xtype: 'button',
            disabled: true,
            id: me.btnEditID,
            text: '修改',
            iconCls: 'icon-btn-edit',
            handler: function () {
                var f = me.getForm("SHApp.Sys.User.Form");
                var rs = me.getSelectionModel().getSelection();
                me.EditData(f);
                f.setFieldValue('cDate', formatDate(rs[0].data.CreateDate));
                f.setFieldValue('mDate', new Date());
                f.down("#nameID").setVisible(false);
            }
        }, ' ', {
            xtype: 'button',
            disabled: true,
            id: me.btnDeleteID,
            text: '删除',
            iconCls: 'icon-btn-delete',
            handler: function () {
                var rs = me.getSelectionModel().getSelection();
                if (rs.length > 0) {
                    if (rs[0].data.Name == "admin") {
                        Ext.Msg.alert("提示", "此用户你没有删除的权限！");
                        return;
                    }
                }
                me.DeleteData();
            }
        }, ' ', {
            xtype: 'button',
            disabled: true,
            id: me.btnLockID,
            text: '锁定',
            iconCls: 'icon-btn-lock',
            handler: function () {
                var grid = this.up("gridpanel");
                var btn = grid.down("#" + me.btnLockID);
                var flag = 1;
                me.LockData(flag, "user?format=json");
            }
        }, ' ', {
            xtype: 'button',
            disabled: true,
            id: me.btnUnLockID,
            text: '解锁',
            iconCls: 'icon-btn-unlock',
            handler: function () {
                var grid = this.up("gridpanel");
                var btn = grid.down("#" + me.btnUnLockID);
                var flag = 0;
                me.LockData(flag, "user?format=json");
            }
        }];
    }
})

